<template>
  <div id="homePage">
    <!--    搜索框-->
    <div class = "search-bar">
      <a-input-search
        v-model:value="searchParams.searchText"
        placeholder="从海量图片中搜索"
        enter-button="搜索"
        size="large"
        @search="doSearch"
      />
    </div>
    <a-tabs v-model:activeKey="selectedCategory" @change = "doSearch">
      <a-tab-pane key = "all" tab = "全部" />
      <a-tab-pane v-for="category in categoryList" :key="category" :tab="category" />
    </a-tabs>
    <div class="tag-bar" style = "margin-bottom: 10px">
      <span style="margin-right: 8px">标签：</span>
      <a-space :size="[0, 8]" wrap>
        <a-checkable-tag
          v-for="(tag, index) in tagList"
          :key="tag"
          v-model:checked="selectedTagList[index]"
          @change="doSearch"
        >
          {{ tag }}
        </a-checkable-tag>
      </a-space>
    </div>
    <!-- 图片列表 -->
    <PictureList :dataList="dataList" :loading="loading" />
    <a-pagination
      style="text-align: right"
      v-model:current="searchParams.current"
      v-model:pageSize="searchParams.pageSize"
      :total="total"
      @change="onPageChange"
    />

  </div>
</template>


<script setup lang="ts">
import {computed, ref, reactive, onMounted} from 'vue'
import {
  listPictureTagCategoryUsingGet,
  listPictureVoByPageUsingPost
} from '@/api/pictureController.ts'
import {message} from "ant-design-vue";
import {useRouter} from "vue-router";
import PictureList from "@/components/PictureList.vue";


// 定义数据
const dataList = ref<API.PictureVO[]>([])
const total = ref(0)
const loading = ref(true)

// 搜索条件
const searchParams = reactive<API.PictureQueryRequest>({
  current: 1,
  pageSize: 12,
  sortField: 'createTime',
  sortOrder: 'descend',
})



// 获取数据
const fetchData = async () => {
  loading.value = true
  //转换搜索参数
  const params = {
    ...searchParams,
   tags:[] as string[],
  }
  if (selectedCategory.value !== 'all') {
    params.category = selectedCategory.value
  }
  selectedTagList.value.forEach((useTag, index) => {
    if (useTag) {
      params.tags.push(tagList.value[index])
    }
  })

  const res = await listPictureVoByPageUsingPost(params)
  if (res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
  loading.value = false
}
const router = useRouter()

//图片详情页
const doClickPicture = (picture: API.PictureVO) => {
  router.push({
    path:`/picture/${picture.id}`,
  })
}


// 分页参数
const onPageChange = (page, pageSize) => {
  searchParams.current = page
  searchParams.pageSize = pageSize
  fetchData()
}


// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.current,
    pageSize: searchParams.pageSize,
    total: total.value,
    onChange: (page:number, pageSize:number) =>{
      searchParams.current = page
      searchParams.pageSize = pageSize
      fetchData()
    }
  }
})

// 页面加载时获取数据，请求一次
onMounted(() => {
  fetchData()
  getTagCategoryOptions()
})

const doSearch = () => {
  //重置搜索条件
  searchParams.current = 1
  fetchData()
}

const categoryList = ref<string[]>([])
const tagList = ref<string[]>([])
const selectedCategory = ref<string>('all')
const selectedTagList = ref<string[]>([])

const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet();
    if(res.data.code === 200 && res.data.data){
        tagList.value = res.data.data.tagList ?? [];
        categoryList.value = res.data.data.categoryList ?? [];
    }else{
      message.error('加载分类标签失败，' + res.data.message)
    }
}


</script>
<style scoped>

#homePage .search-bar{
  max-width: 480px;
  margin: 0 auto 16px;
}

</style>
